var arrayDestructuring = function(inputArray, limit) {
    if (Array.isArray(inputArray)) return inputArray;
    if (Symbol.iterator in Object(inputArray)) return function(iterable, limit) {
        var resultArray = [], shouldContinue = !0, errorOccurred = !1, error = void 0;
        try {
            for (var iterator, step; !(shouldContinue = (step = iterable[Symbol.iterator]()).next().done) && (resultArray.push(step.value), 
            !limit || resultArray.length !== limit); shouldContinue = !0) ;
        } catch (err) {
            errorOccurred = !0, error = err;
        } finally {
            try {
                !shouldContinue && step.return && step.return();
            } finally {
                if (errorOccurred) throw error;
            }
        }
        return resultArray;
    }(inputArray, limit);
    throw new TypeError("Invalid attempt to destructure non-iterable instance");
}, component = require("../common/component"), pickerShared = require("../picker/shared");

(0, component.VantComponent)({
    classes: [ "active-class", "toolbar-class", "column-class" ],
    props: Object.assign({}, pickerShared.pickerProps, {
        value: String,
        areaList: {
            type: Object,
            value: {}
        },
        columnsNum: {
            type: [ String, Number ],
            value: 3
        }
    }),
    data: {
        columns: [ {
            values: []
        }, {
            values: []
        }, {
            values: []
        } ],
        displayColumns: [ {
            values: []
        }, {
            values: []
        }, {
            values: []
        } ]
    },
    watch: {
        value: function(newValue) {
            this.code = newValue, this.setValues();
        },
        areaList: "setValues",
        columnsNum: function(newColumnsNum) {
            this.set({
                displayColumns: this.data.columns.slice(0, +newColumnsNum)
            });
        }
    },
    mounted: function() {
        this.setValues();
    },
    methods: {
        getPicker: function() {
            return null == this.picker && (this.picker = this.selectComponent(".van-area__picker")), 
            this.picker;
        },
        onCancel: function(event) {
            this.emit("cancel", event.detail);
        },
        onConfirm: function(event) {
            this.emit("confirm", event.detail);
        },
        emit: function(eventName, eventData) {
            eventData.values = eventData.value, delete eventData.value, this.$emit(eventName, eventData);
        },
        onChange: function(event) {
            var self = this, detail = event.detail, columnIndex = detail.index, picker = detail.picker, selectedValue = detail.value;
            this.code = selectedValue[columnIndex].code, this.setValues().then(function() {
                self.$emit("change", {
                    picker: picker,
                    values: picker.getValues(),
                    index: columnIndex
                });
            });
        },
        getConfig: function(configType) {
            var areaList = this.data.areaList;
            return areaList && areaList[configType + "_list"] || {};
        },
        getList: function(listType, codePrefix) {
            var resultList = [];
            if ("province" !== listType && !codePrefix) return resultList;
            var config = this.getConfig(listType);
            return resultList = Object.keys(config).map(function(code) {
                return {
                    code: code,
                    name: config[code]
                };
            }), codePrefix && ("9" === codePrefix[0] && "city" === listType && (codePrefix = "9"), resultList = resultList.filter(function(item) {
                return 0 === item.code.indexOf(codePrefix);
            })), resultList;
        },
        getIndex: function(listType, code) {
            var length = "province" === listType ? 2 : "city" === listType ? 4 : 6, list = this.getList(listType, code.slice(0, length - 2));
            "9" === code[0] && "province" === listType && (length = 1), code = code.slice(0, length);
            for (var index = 0; index < list.length; index++) if (list[index].code.slice(0, length) === code) return index;
            return 0;
        },
        setValues: function() {
            var self = this, areaConfig = this.getConfig("county"), defaultCode = this.code || Object.keys(areaConfig)[0] || "", provinceList = this.getList("province"), cityList = this.getList("city", defaultCode.slice(0, 2)), picker = this.getPicker();
            if (picker) {
                var promises = [];
                if (promises.push(picker.setColumnValues(0, provinceList, !1)), promises.push(picker.setColumnValues(1, cityList, !1)), cityList.length && "00" === defaultCode.slice(2, 4)) {
                    var firstCity = arrayDestructuring(cityList, 1);
                    defaultCode = firstCity[0].code;
                }
                return promises.push(picker.setColumnValues(2, this.getList("county", defaultCode.slice(0, 4)), !1)), 
                Promise.all(promises).catch(function() {}).then(function() {
                    return picker.setIndexes([ self.getIndex("province", defaultCode), self.getIndex("city", defaultCode), self.getIndex("county", defaultCode) ]);
                }).catch(function() {});
            }
        },
        getValues: function() {
            var picker = this.getPicker();
            return picker ? picker.getValues().filter(function(value) {
                return !!value;
            }) : [];
        },
        getDetail: function() {
            var values = this.getValues(), detail = {
                code: "",
                country: "",
                province: "",
                city: "",
                county: ""
            };
            if (!values.length) return detail;
            var names = values.map(function(value) {
                return value.name;
            });
            return detail.code = values[values.length - 1].code, "9" === detail.code[0] ? (detail.country = names[1] || "", 
            detail.province = names[2] || "") : (detail.province = names[0] || "", detail.city = names[1] || "", detail.county = names[2] || ""), 
            detail;
        },
        reset: function() {
            return this.code = "", this.setValues();
        }
    }
});